package integral;

import models.BaseModel;
import models.weixin.WebUser;

import javax.persistence.*;
import java.util.Date;

/**
 * 积分消费记录
 * Created by youliangcheng on 17/11/18.
 */
@Entity
@Table(name = "integral_exchange_historys")
public class IntegralExchangeHistory extends BaseModel {

    /**
     * 关联用户
     */
    @ManyToOne
    @JoinColumn(name = "web_user_id")
    public WebUser webUser;


    /**
     * 使用掉的积分数
     */
    @Column(name = "integral_used_num")
    public Integer integralUsedNum;


    /**
     * 剩余积分数
     */
    @Column(name = "integral_surplus_num")
    public Integer integralSurplusNum;

    /**
     * 扣除积分的订单ID
     */
    @Column(name = "order_id")
    public Long orderId;

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    public static IntegralExchangeHistory findByHistoryId(long id){
        return IntegralExchangeHistory.find("id=? " , id).first();
    }

    public static void createHistory(WebUser webUser , Integer integralUsedNum , Long orderId){
        IntegralExchangeHistory exchangeHistory = new IntegralExchangeHistory();
        exchangeHistory.webUser = webUser ;
        exchangeHistory.integralUsedNum = integralUsedNum;
        exchangeHistory.integralSurplusNum = webUser.integrals;
        exchangeHistory.orderId = orderId;
        exchangeHistory.createAt = new Date();
        exchangeHistory.save();

    }










}
